package com.slkj.shilixiaoyuanapp.ui.main.message;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.BaseLazyFragment;
import com.slkj.shilixiaoyuanapp.entity.MessageContactsGroupEntity;
import com.slkj.shilixiaoyuanapp.entity.contacts.MailListEntity;
import com.slkj.shilixiaoyuanapp.entity.contacts.PersonBean;
import com.slkj.shilixiaoyuanapp.entity.contacts.Student;
import com.slkj.shilixiaoyuanapp.net.HttpConfig;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.view.DividerItemDecoration;
import com.slkj.shilixiaoyuanapp.view.StateLayout;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created  on 2018-07-18
 *
 * @author 唐开阔
 * @describe 消息-联系人
 */
public class MessageContactsFragment extends BaseLazyFragment {

    @BindView(R.id.recycer)
    RecyclerView recycerView;
    MessageContactsAdater adapter;
    List<MultiItemEntity> contactsList = new ArrayList<>();
    ArrayList<PersonBean> personBeanList = new ArrayList<>();
    @BindView(R.id.statelayout)
    StateLayout stateLayout;
    @Override
    protected int getResId() {
        return R.layout.fragment_message_contacts;
    }

    @Override
    protected void onRealLoaded() {
        stateLayout.setOnReLoadListener(this::initData);
        recycerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recycerView.addItemDecoration(new DividerItemDecoration(getContext(),LinearLayoutManager.VERTICAL));
        adapter = new MessageContactsAdater(getActivity(),contactsList);
        recycerView.setAdapter(adapter);
        initData();
    }
    private void initData() {
        contactsList.clear();
        personBeanList.clear();
        HttpHeper.get().mainPageService().getMailList()
                .map(result -> {
                    MailListEntity data = result.getData();
                    if (HttpConfig.RESPONSE_OK.equals(result.getStatus())) {
                        //老师组
                        String name = data.getTeacher().getName();
                        MessageContactsGroupEntity group = new MessageContactsGroupEntity(name);
                        //组下列表
                        for (int i = 0; i < data.getTeacher().getList().size(); i++) {
                            PersonBean personBean = data.getTeacher().getList().get(i);
                            personBeanList.add(personBean);
                            group.addSubItem(personBean);
                        }
                        contactsList.add(group);
                        //学生组
                        for (int i = 0; i < data.getStudent().size(); i++) {
                            Student student = data.getStudent().get(i);
                            String name1 = student.getName();
                            MessageContactsGroupEntity group1 = new MessageContactsGroupEntity(name1);
                            //组下列表
                            for (int j = 0; j < data.getStudent().get(i).getList().size(); j++) {
                                PersonBean personBean = data.getStudent().get(i).getList().get(j);
                                personBeanList.add(personBean);
                                group1.addSubItem(personBean);
                            }
                            contactsList.add(group1);
                        }
                    }
                    return result;
                })
                .compose(getThread())
                .compose(bindToLifecycle())
                .subscribe(new CommonCallBack<MailListEntity>(stateLayout) {
                    @Override
                    public void onCallBackSuccess(MailListEntity data) {
                        adapter.notifyDataSetChanged();
                        stateLayout.showContentView();
                    }


                });
    }
    @OnClick(R.id.layout_search)
    void toSearch(){
        Intent intent = new Intent(getActivity(), SearchActivity.class);
        intent.putExtra("pearsonList",(Serializable) personBeanList);
        startActivity(intent);
    }

}
